package com.difsonapps.android.marker.adapters;

import com.difsonapps.android.marker.AppConstant;
import com.difsonapps.android.marker.FeedImageView;
import com.difsonapps.android.marker.FormAlbumPhotosActivity;
import com.difsonapps.android.marker.MainActivity;
import com.difsonapps.android.marker.ProfileActivity;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.Friend;
import com.difsonapps.android.marker.fragments.FriendFragment;
import com.difsonapps.android.marker.util.MarkerUtil;
 
import java.util.List;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.text.Html;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
 
public class FriendListAdapter extends BaseAdapter {  
    private Activity activity;
    private LayoutInflater inflater;
    private List<Friend> friendItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    
    String avgSym = "";
    String no_mark = "";
    String mark = "";
    String marks = "";
    protected FriendFragment friendFragment;
 
    public FriendListAdapter(Activity activity, List<Friend> friendItems, FriendFragment friendFragment) {
        this.activity = activity;
        this.friendItems = friendItems;
        this.friendFragment = friendFragment;
        
        avgSym = activity.getResources().getString(R.string.avg_sym);
        no_mark = activity.getResources().getString(R.string.no_mark);
        mark = activity.getResources().getString(R.string.mark);
        marks = activity.getResources().getString(R.string.marks);
    }
 
    @Override
    public int getCount() {
        return friendItems.size();
    }
 
    @Override
    public Object getItem(int location) {
        return friendItems.get(location);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
 
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.friend_item, null);
 
        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
 
        TextView name = (TextView) convertView.findViewById(R.id.name);
        TextView timestamp = (TextView) convertView.findViewById(R.id.timestamp);
        NetworkImageView profilePic = (NetworkImageView) convertView.findViewById(R.id.profilePic);
        ImageView delete_button = (ImageView) convertView.findViewById(R.id.delete_button);
         
        Friend item = friendItems.get(position);

        if(item.getUserTo()!=null){
        	ClickProfileListener profilListener = new ClickProfileListener(position);
        	name.setText(item.getUserTo().getName());
        	profilePic.setImageUrl(item.getUserTo().getPictureUrl(), imageLoader);
        	profilePic.setOnClickListener(profilListener);
        	name.setOnClickListener(profilListener);
        }
        timestamp.setText(item.getConfirmAt());
        delete_button.setOnClickListener(new ClickDeleteButtonListener(position));
 
        return convertView;
    }
    
    class ClickProfileListener implements View.OnClickListener{
    	int position;
    	

		public ClickProfileListener(int position) {
			super();
			this.position = position;
		}


		@Override
		public void onClick(View v) {
			deleteFriend(position);
		}
    	
    }
    public void deleteFriend(final int position){
    	new AlertDialog.Builder(activity)
    	.setTitle("Delete friend")
    	.setMessage("Do you really want to delete this friend?")
    	.setIcon(android.R.drawable.ic_dialog_alert)
    	.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

    	    public void onClick(DialogInterface dialog, int whichButton) {
    	    	confirmDeleteFriend(position);
    	    }})
    	 .setNegativeButton(android.R.string.no, null).show();
    }
    public void confirmDeleteFriend(int position){
    	Friend feedItem = FriendListAdapter.this.friendItems.get(position);
		if(feedItem!=null && feedItem.getUserTo()!=null){
			Intent intent = new Intent(FriendListAdapter.this.activity, ProfileActivity.class);
			Bundle b = new Bundle();
			b.putParcelable("user", feedItem.getUserTo());
			intent.putExtras(b);
			FriendListAdapter.this.activity.startActivity(intent);
		}
    }
    class ClickDeleteButtonListener implements View.OnClickListener{
    	int position;
    	

		public ClickDeleteButtonListener(int position) {
			super();
			this.position = position;
		}


		@Override
		public void onClick(View v) {
			Friend friend = FriendListAdapter.this.friendItems.get(position);
			loadDeleteFriend(friend);
		}
    	
    }
    
    private void loadDeleteFriend(Friend friend){
    	if(friendFragment!=null){
    		friendFragment.loadDeleteFriend(friend); 
    	}
    }
 
}
